package com.example.home;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.RecyclerView;

import com.example.common.control.ICallback;
import com.example.common.user.GlobalUser;
import com.example.home.databinding.HomeRecyclerItemBinding;
import com.example.home.model.UserLike;
import com.example.home.viewmodel.HomeRecommendViewModel;
import com.example.home.viewmodel.HomeViewModel;

import org.jetbrains.annotations.NotNull;

public class RecommendRAdapter extends RecyclerView.Adapter<RecommendRAdapter.RecommendRHolder> {


    private static final String TAG = "RecommendRAdapter";
    HomeRecommendViewModel viewModel;
    private Context context;
    //点击跳转接口
    public IStartGameDetailActivity iStartGameDetailActivity;


    //构造函数初始化获取ViewModel
    public RecommendRAdapter(HomeRecommendViewModel homeRecommendViewModel, Context context) {
        this.viewModel = homeRecommendViewModel;
        this.context = context;
    }

    @Override
    public RecommendRHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        HomeRecyclerItemBinding homeRecyclerItemBinding = DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()), R.layout.home_recycler_item, parent, false);

        //绑定生命周期
        homeRecyclerItemBinding.setLifecycleOwner((LifecycleOwner) context);
        //创建实例并返回
        RecommendRHolder holder = new RecommendRHolder(homeRecyclerItemBinding);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull RecommendRAdapter.RecommendRHolder holder, int position) {
        Log.i(TAG, "onBindViewHolder: " + position);
        //数据绑定
        holder.binding.setData(viewModel.getGames().get(position));

        holder.binding.homeRecommendCv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //通过id打开
                iStartGameDetailActivity.openById(holder.binding.getData().getGame().getId());
            }
        });
        //设置标签
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < (Math.min(holder.binding.getData().getCategory().size(), 3)); i++) {
            if (holder.binding.getData().getCategory().get(i).getName().equals("TapTap独家")) {
                str.append("BatBat独家！！！");
            } else {
                str.append(holder.binding.getData().getCategory().get(i).getName());
            }
            str.append("  ");
        }
        holder.binding.homeRecommendCardGameCategories.setText(str);

        //设置收藏按钮
        Integer id = holder.binding.getData().getGame().getId();
        //验证是否已收藏
        if (GlobalUser.getInstance().getLikeMap().get(id) != null) {
            holder.binding.homeRecommendLikeBt.setChecked(true);
        } else {
            holder.binding.homeRecommendLikeBt.setChecked(false);
        }
        //点击收藏
        holder.binding.homeRecommendLikeBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(holder.binding.homeRecommendLikeBt.isChecked()){
                    viewModel.addFavor(id);
                }else {
                    viewModel.deleteFavor(id);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (viewModel.getGames() == null) {
            return 0;
        } else {
            return viewModel.getGames().size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    /***************************************************************************************************************/
    /**
     * 该adapter的holder，一个holder对应一个item
     */
    static class RecommendRHolder extends RecyclerView.ViewHolder {

        private HomeRecyclerItemBinding binding;

        public RecommendRHolder(@NonNull @NotNull HomeRecyclerItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;//传递binding
        }

    }
}
